package com.example.lettcode._202412._20241211;

/*
168. Excel 表列名称
给你一个整数 columnNumber ，返回它在 Excel 表中相对应的列名称。
例如：
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...


示例 1：
输入：columnNumber = 1
输出："A"
示例 2：

输入：columnNumber = 28
输出："AB"
示例 3：

输入：columnNumber = 701
输出："ZY"
示例 4：

输入：columnNumber = 2147483647
输出："FXSHRXW"
 */
public class _168_excel_biao_lie_ming_cheng {

    public static void main(String[] args) {
        System.out.println(convertToTitle2(52));
    }

    public static String convertToTitle(int columnNumber) {
        String[] sts = {"A", "B", "C", "D", "E", "F","G", "H", "I", "J", "K", "L","M", "N", "O", "P", "Q", "R","S", "T", "U", "V", "W", "X", "Y", "Z"};
        int length = sts.length;
        String ret = "";
        while (columnNumber / length > 0) {
            int temp = columnNumber % length == 0 ? 26 : columnNumber % length;
            ret = sts[temp-1] + ret;
            if (columnNumber / length == 1 && columnNumber % length == 0) {
                return ret;
            } else {
                columnNumber = (columnNumber - temp) / length;
            }
        }

        int temp = columnNumber % length == 0 ? 26 : columnNumber % length;
        ret = sts[temp-1] + ret;

        return ret;
    }

    public static String convertToTitle2(int columnNumber) {
        StringBuffer sb = new StringBuffer();
        while (columnNumber > 0) {
            int a0 = (columnNumber - 1) % 26;
            sb.append((char)(a0 + 'A'));
            columnNumber = (columnNumber - a0) / 26;
        }
        return sb.reverse().toString();
    }
}
